/*
 * Copen1yright 2009 Cedric Priscal
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.beidouin.GnssMain.sample;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.beidouin.GnssMain.SerialPortReader;
import com.beidouin.GnssMain.R;
import com.beidouin.GnssMain.utils.DialogUtil;

import de.greenrobot.event.EventBus;

public abstract class SerialPortActivity extends AppCompatActivity {

	static Boolean bEventRegisted = false;
	static Boolean bEventChange = false;
	static SerialPortActivity lastActivity;


	RelativeLayout basicInfoLayout, satelliteInfoLayout,nmeaInfoLayout,mapInfoLayout,consoleInfoLayout;
	ImageView navigationImage, satelliteImage ,nmeaImage,mapImage,consoleImage;
	TextView navigationText, satelliteText,nmeaText,mapText,consoleText;
	private boolean mIsResumed = false;
	int activityType = -1;

	private void DisplayError(int resourceId) {
		AlertDialog.Builder b = new AlertDialog.Builder(this);
		b.setTitle("Error");
		b.setMessage(resourceId);
		b.setPositiveButton("OK", new OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				SerialPortActivity.this.finish();
			}
		});
		b.show();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Application.getInstance().addActivity(this);

		if (bEventRegisted == false) {
			EventBus.getDefault().register(this);
			bEventRegisted = true;
		}


	}
	protected void initBottomView(){
		basicInfoLayout = (RelativeLayout) findViewById(R.id.basic_info);
		satelliteInfoLayout = (RelativeLayout) findViewById(R.id.satellite_info);
		nmeaInfoLayout= (RelativeLayout) findViewById(R.id.nmea_info);
		mapInfoLayout= (RelativeLayout) findViewById(R.id.map_info);
		consoleInfoLayout= (RelativeLayout) findViewById(R.id.console_info);


		navigationImage = (ImageView) findViewById(R.id.navigation_iv);
		satelliteImage = (ImageView) findViewById(R.id.satellite_iv);
		nmeaImage= (ImageView) findViewById(R.id.nmea_iv);
		mapImage= (ImageView) findViewById(R.id.map_iv);
		consoleImage= (ImageView) findViewById(R.id.console_iv);


		navigationText = (TextView) findViewById(R.id.navigation_tv);
		satelliteText = (TextView) findViewById(R.id.satellite_tv);
		nmeaText = (TextView) findViewById(R.id.nmea_tv);
		mapText= (TextView) findViewById(R.id.map_tv);
		consoleText= (TextView) findViewById(R.id.console_tv);


		basicInfoLayout.setOnClickListener(navigateListener);
		satelliteInfoLayout.setOnClickListener(navigateListener);
		nmeaInfoLayout.setOnClickListener(navigateListener);
		mapInfoLayout.setOnClickListener(navigateListener);
		consoleInfoLayout.setOnClickListener(navigateListener);


		navigationImage.setImageResource(R.drawable.navigation);
		satelliteImage.setImageResource(R.drawable.satelite);
		nmeaImage.setImageResource(R.drawable.icon_nmea);
		mapImage.setImageResource(R.drawable.icon_map);
		consoleImage.setImageResource(R.drawable.action);
	}
	View.OnClickListener navigateListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			RelativeLayout target =  (RelativeLayout)v;
			switch (target.getId()) {
				case R.id.basic_info:
					Intent bIntent = new Intent(getCurrentActivity(), MainMenu.class);
					startActivity(bIntent);
					break;
				case R.id.satellite_info:
					Intent sIntent = new Intent(getCurrentActivity(), SatelliteActivity.class);
					startActivity(sIntent);
					break;
				case R.id.nmea_info:
					Intent nIntent = new Intent(getCurrentActivity(),NmeaActivity.class);
					startActivity(nIntent);
					break;
				case R.id.map_info:
					Intent mIntent = new Intent(getCurrentActivity(),MapActivity.class);
					startActivity(mIntent);
					break;
				case R.id.console_info:
					Intent cIntent = new Intent(getCurrentActivity(),ConsoleActivity.class);
					startActivity(cIntent);
					break;
			}
		}
	};

	/**
	 * can't use isResumed(), seems conflict with system method, will cause linkage error
	 */
	public boolean hasResumed() {
		return mIsResumed;
	}
	@Override
	protected void onResume() {
		if (bEventChange == true) {
			bEventChange = false;
			EventBus.getDefault().unregister(lastActivity);
			EventBus.getDefault().register(this);
		}
		super.onResume();
		mIsResumed = true;
	}
	@Override
	protected void onPause() {
		super.onPause();
		mIsResumed = false;
		bEventChange = true;
		lastActivity = this;
	}

	protected Activity getCurrentActivity(){
		return SerialPortActivity.this;
	}

	@Override
	protected void onDestroy() {
		EventBus.getDefault().unregister(this);
		super.onDestroy();

	}

	@Override
	public void onBackPressed() {
		DialogUtil.showConfirmDialog(this, "提示", "确定退出应用么?",
				DialogUtil.ButtonSettings.POSITIVE_AND_NEGATIVE, new DialogUtil.DialogCallback() {
					@Override
					public void onNegativeButtonClicked() {
						return;
					}

					@Override
					public void onPositiveButtonClicked() {
						SerialPortReader.getInstance().CloseSerialPort();
						SerialPortReader.getInstance().CloseSerialPort2();
						SerialPortReader.getInstance().StopThread1();
						SerialPortReader.getInstance().StopThread2();
						EventBus.getDefault().unregister(this);
						Application.getInstance().onTerminate();
					}
				});
	}
}
